﻿<%@ WebHandler Language="C#" Class="MesToErpReportData" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description:MES-ERP报工数据查询报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * create Date:2022.03.30
 */
public class MesToErpReportData : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    Result getData(JObject jObject)
    {
        string sql = getSearchSql(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dtResult = dto.DBTable;
        foreach (DataColumn col in dtResult.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        Result r = new Result();
        r.data = dtResult;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //查询方法
    string getSearchSql(JObject jObject)
    {
       string sql = @"
SELECT t.mfgordername,t.containername,ws.workflowstepname,t.specno,t.reportdate,t.reportqty,t.lossqty,t.createdate,
       DECODE(t.status,1,'成功','') status,DECODE(t.isuse,1,'撤销','正常') isuse
FROM mes_workingreport_table t 
INNER JOIN workflowstep ws ON ws.workflowstepid = SUBSTR(t.containerstep,17,16)
WHERE 1=1 ";
        //订单号
        if (jObject["mfgordername"] != null && jObject["mfgordername"].ToString() != "")
        {
            sql += string.Format(" AND t.mfgordername = '{0}'", jObject["mfgordername"].ToString());
        }

        //流水卡号
        if (jObject["containername"] != null && jObject["containername"].ToString() != "")
        {
            sql += string.Format(" AND t.containername = '{0}'", jObject["containername"].ToString());
        }

        //工序号
        if (jObject["specno"] != null && jObject["specno"].ToString() != "")
        {
            sql += string.Format(" AND t.specno LIKE '%{0}%'", jObject["specno"].ToString());
        }

        //创建时间
        if (jObject["createdate"] != null && jObject["createdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["createdate"].ToString());
            sql += string.Format(" AND t.createdate >= to_date('{0} 00:00:00','yyyy-MM-dd hh24:mi:ss')", arr[0].ToString());
            sql += string.Format(" AND t.createdate <= to_date('{0} 00:00:00','yyyy-MM-dd hh24:mi:ss')", arr[1].ToString());
        }
        return sql;
    }

    void exportExcel(JObject jObject)
    {
        string sql = getSearchSql(jObject);
        DataTable dt = dal.GetDataTable(sql);
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        dt.Columns["mfgordername"].Caption = "订单号";
        dt.Columns["containername"].Caption = "流水卡号";
        dt.Columns["workflowstepname"].Caption = "工序";
        dt.Columns["specno"].Caption = "工序号";
        dt.Columns["reportdate"].Caption = "检验日期";
        dt.Columns["reportqty"].Caption = "合格数";
        dt.Columns["lossqty"].Caption = "报废数";
        dt.Columns["createdate"].Caption = "创建日期";
        dt.Columns["status"].Caption = "状态";
        dt.Columns["isuse"].Caption = "撤销状态";
        ExportHelper.ExportExcel(dt, "MES-ERP报工数据查询报表");
    }
}